<?php
// +----------------------------------------------------------------------
// | 品达网络 [ 品质保证，助企腾达 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.pdwl.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 萧涩啊 <wangmode@163.com>
// +----------------------------------------------------------------------

namespace app\services;

class JsonService
{
    private static $SUCCESSFUL_DEFAULT_MSG = 'ok';

    private static $FAIL_DEFAULT_MSG = 'no';

    const CODE_TOKEN_FAIL = 401;
    
    const CODE_BIND_FAIL = 402;

    public static function result($code, $msg = '', $data = [], $count = 0)
    {
        // header('Content-Type: application/json');
        exit(json_encode(compact('code', 'msg', 'data', 'count')));
    }

    public static function successLayui($count = 0, $data = [], $msg = '')
    {
        if (is_array($count)) {
            if (isset($count['data'])) $data = $count['data'];
            if (isset($count['count'])) $count = $count['count'];
        }

        if (false == is_string($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        self::result(1, $msg, $data, $count);
    }

    public static function successful($msg = 'ok', $data = [], $status = 1)
    {
        if (false == is_string($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        self::result($status, $msg, $data);
    }

    public static function status($status, $msg, $result = [])
    {
        $status = strtoupper($status);
        if (true == is_array($msg)) {
            $result = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        self::result(1, $msg, compact('status', 'result'));
    }

    public static function fail($msg, $data = [], $code = 0)
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$FAIL_DEFAULT_MSG;
        }
        self::result($code, $msg, $data);
    }

    public static function success($msg, $data = [])
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        self::result(1, $msg, $data);
    }


    public static function tokenFail($msg, $data = [])
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        self::result(self::CODE_TOKEN_FAIL, $msg, $data);
    }
    
    public static function bindFail($msg, $data = [])
    {
        if (true == is_array($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MSG;
        }
        self::result(self::CODE_BIND_FAIL, $msg, $data);
    }



    /*
     * 设置返回数据
     * @param int $code 响应code
     * @param string $msg 提示语
     * @param array $data 返回数据
     * @return array
     * */
    public static function returnData($code, $msg = '', $data = [])
    {
        return compact('code', 'msg', 'data');
    }


}